home *** CD-ROM | disk | FTP | other *** search
- Path: lrz-muenchen.de!news
- From: watzka@stat.uni-muenchen.de (Kurt Watzka)
- Newsgroups: comp.lang.c
- Subject: Re: Probs with prototype mixes with Functions
- Date: 21 Apr 1996 14:51:59 GMT
- Organization: Leibniz-Rechenzentrum, Muenchen (Germany)
- Distribution: world
- Message-ID: <4ldi2f$6ed@sparcserver.lrz-muenchen.de>
- References: <dward.1.000DC9CA@melbpc.org.au> <31790B9E.3EE@ix.netcom.com>
- NNTP-Posting-Host: sun2.lrz-muenchen.de
-
- Jane Harper <jharper@ix.netcom.com> writes:
-
- >Darren Ward wrote:
- >>
- >> Could anyone help me with the following?
- >>
- >> when making a function such as:
- >>
- >> float convert(int celcius)
- >> {
- >> float faranheit;
- >> faranheit = ((float)celcius*9/5)+32;
- >> return faranheit;
- >> }
-
- >When you multiply celcius (cast to float) by 9/5 (an integer division),
- >celcius is cast back to an int. Therefore, your return is an int, not a
- >float. If you want a float return, change 9/5 to 9.0/5.0. :D
-
- While there indeed _is_ a potential problem using 9/5 to represent 1.8,
- a float will _never_ be cast back to an int during the evaluation of
- an expression if the expression is anything but an assignment expression.
-
- So, if you write
-
- float fahrenheit;
- int celsius;
-
- fahrenheit = (float) celsius * 9;
-
- you can be sure that the multiplication is done with either float or
- double prcision, but _not_ using integer arithmetic.
-
- Kurt
- --
- | Kurt Watzka Phone : +49-89-2180-6254
- | watzka@stat.uni-muenchen.de
-